package com.summer.order.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityQuery;
import com.summer.core.controller.LoginUserInfo;
import com.summer.order.model.DayFood;
import com.summer.order.model.UserDayFood;
import com.summer.order.model.UserOrder;


@Component
@Scope(ScopeType.VIEW)
public class MyOrderList extends EntityQuery<UserOrder>{

	@Resource
	LoginUserInfo loginUserInfo;
	
	private List<UserDayFood> userDayFoodList;
	
	private UserOrder userOrder;
	
	
	private final String HQL = "select userOrder from UserOrder userOrder";
	private final String COUNT_HQL = "select count(userOrder) from UserOrder userOrder";
	
	public MyOrderList() {
		
		
		setCountHql(COUNT_HQL);
		setHql(HQL);
		final String[] RESTRICTIONS = {				
				"userOrder.user.id = #{loginUserInfo.loginUserId}",				
				};
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	}

	@SuppressWarnings("unchecked")
	public List<UserDayFood> getUserDayFoodList() {
		
		if(userOrder == null) {
			return null;
		}
		userDayFoodList = getHt().find("from UserDayFood userDayFood "
				+ "where userDayFood.userOrder.id =?",userOrder.getId());
		
		return userDayFoodList;
	}
	
	public String getFoodName(Date date) {
		StringBuffer foodName = new StringBuffer();
		
		if(date != null) {
			List<DayFood> dayFoodList = getHt()
					.find("select dayFood from DayFood dayFood where dayFood.date=?"
							,date);
			if(dayFoodList != null && dayFoodList.size() > 0) {
				DayFood dayFood = dayFoodList.get(0);
				if(dayFood.getFood1() != null) {
					foodName.append(dayFood.getFood1().getName());
				}
				if(dayFood.getFood2() != null) {
					foodName.append(" + "+dayFood.getFood2().getName());
				}
			}
		}
		return foodName.toString();
	}

	public void setUserDayFoodList(List<UserDayFood> userDayFoodList) {
		this.userDayFoodList = userDayFoodList;
	}

	public UserOrder getUserOrder() {
		return userOrder;
	}

	public void setUserOrder(UserOrder userOrder) {
		this.userOrder = userOrder;
	}
	
	
	
	
	
	

}
